<template>
    <div class="app-container">
        <div style="text-align: center;margin-bottom: 30px">
            消防企业{{createStatus?'新增':'更新'}}
        </div>
        <el-form :model="fireCompany"  ref="fireCompany" :rules="fireCompanyRules" label-width="100px">
            <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="消防站名称" prop="name">
                            <el-input type="text" v-model="fireCompany.name" placeholder="请输入消防站名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="消防站编码"  prop="code">
                            <el-input v-model="fireCompany.code" placeholder="请输入消防站编码"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="消防站类型 " prop="enterpriseType">
                            <el-select v-model="fireCompany.type" placeholder="消防站类型" style="width: 100%">
                                <el-option :value="0" label="总队"></el-option>
                                <el-option :value="1" label="支队"></el-option>
                                <el-option :value="2" label="大队"></el-option>
                                <el-option :value="3" label="中队"></el-option>
                                <el-option :value="4" label="微型消防站 "></el-option>
                                <el-option :value="5" label="消控室"></el-option>
                                <el-option :value="6" label="警务站"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="省市县" prop="fireCompanyLocation">
                            <city-select :local="fireCompany.fireCompanyLocation" v-on:handleChange="getCity"></city-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="主管人" prop="person">
                            <el-input type="text" v-model="fireCompany.person" placeholder="请输入企业负责人" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="主管人电话" prop="tel">
                            <el-input type="text" v-model="fireCompany.tel" placeholder="请输入主管人电话" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="消防站电话" prop="mobile">
                            <el-input type="text" v-model="fireCompany.mobile" placeholder="请输入消防站电话" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="详细地址" prop="address">
                            <el-input type="text" v-model="fireCompany.address" placeholder="请输入详细地址" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-col>
            </el-row>
        </el-form>

        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelFireCompany">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createFireCompany">创 建</el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updateFireCompany">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {addInFireCompany, getInFireCompanyVo, updateInFireCompany} from "@/api/fireConmany";
import CitySelect from "@/components/citySelect/index.vue";

export default {
    name: "InFireCompanyEdit",
    components: {CitySelect},
    data(){
        return{
            fireCompany:{

            },
            fireCompanyRules:{
                mobile: [{required: true, message: '消防站电话不能为空', trigger: 'blur'}],
                address: [{required: true, message: '详细地址不能为空', trigger: 'blur'}],
                name: [{required: true, message: '消防站名称不能为空', trigger: 'blur'}],

            },
            addLoading:false,
            imgFlag:false,
            imgUploadPercent:0
        }
    },
    computed:{
        createStatus(){
            return this.$route.params.ucId == '0'
        },
    },
    created() {
        if (!this.createStatus){
            let ucId = this.$route.params.ucId
            console.log(ucId)
            this.getInFireCompanyVo(ucId)
        }
    },
    methods:{
        getInFireCompanyVo(val){
            getInFireCompanyVo({ucId:val}).then(res=>{
                this.fireCompany = res.data
                this.fireCompany.fireCompanyLocation = [this.fireCompany.province,this.fireCompany.city,this.fireCompany.county]
            }).catch(res=>{

            })
        },
        cancelFireCompany(){
          window.history.back()
        },
        createFireCompany(){
            this.addLoading = true
            this.$refs['fireCompany'].validate(valid => {
                if (valid){
                    addInFireCompany(this.fireCompany).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加消防企业成功"
                        })
                        this.cancelFireCompany()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateFireCompany(){
            this.addLoading = true
            this.$refs['fireCompany'].validate(valid => {
                if (valid){
                    updateInFireCompany(this.fireCompany).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新消防企业信息成功"
                        })
                        this.cancelFireCompany()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        //选择位置信息
        getCity(val){
            this.fireCompany.fireCompanyLocation = val
            this.fireCompany.province=val[0]
            this.fireCompany.city=val[1]
            this.fireCompany.county=val[2]
        }
    }

}
</script>



